package yxk.service.configm;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import yxk.utils.BosUtil;
import yxk.utils.HttpRequestUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * OA方法服务类
 */
public class OAService{
    private static final Log log = LogFactory.getLog(OAService.class);

    private DynamicObject OADBLC;

    /**
     * 代办流程
     * @param param     请求参数
     * @return
     */
    public String requestOAInfoBy(String param){
        DynamicObject oadblc = getOADBLC();
        String nwg5Url = oadblc.getString("nwg5_url");
        log.info("requestOAInfoBy.nwg5Url:{}",nwg5Url);
        try{
            String result = HttpRequestUtil.getPostResult(nwg5Url, null, param, "application/json");
            return result;
        }catch (Exception e){
            log.info("OAService.requestOAInfoBy.e:{}",e);
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除代办任务
     * @param param     请求参数
     * @return
     */
    public String deleteUserRequestInfoBy(String param){
        DynamicObject oadblc = getOADBLC();
        String secretUrl = oadblc.getString("nwg5_secreturl");
        try{
            log.info("OAService.deleteUserRequestInfoBy.param:{}",param);
            String result = HttpRequestUtil.getPostResult(secretUrl, null, param, "application/json");
            log.info("OAService.deleteUserRequestInfoBy.result:{}",result);
            return result;
        }catch (Exception e){
            log.info("OAService.deleteUserRequestInfoBy.e:{}",e);
            e.printStackTrace();
        }
        return null;
    }

    public DynamicObject getOADBLC() {
        if(OADBLC == null){
            OADBLC = BosUtil.getConfigUration("OADBLC");
        }
        return OADBLC;
    }

    public static List<String> getSelectColumByIds(String entryName, String colum, List<Long> ids){
        QFilter filter = new QFilter("id", QCP.in, ids);
        List<String> colList = new ArrayList<>();
        Map<Object, DynamicObject> objectMap = BusinessDataServiceHelper.loadFromCache(entryName, colum, new QFilter[]{filter});
        if(objectMap != null && objectMap.size() > 0){
            List<DynamicObject> collect = objectMap.values().stream().collect(Collectors.toList());
            for(DynamicObject object : collect){
                String col = object.getString(colum);
                if(!colList.contains(col)){
                    colList.add(col);
                }
            }
        }
        return colList;
    }
}